package com.gtapps.user.service;

import com.gtapps.base.utils.AllTest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

@Aspect
@Component
public class AllTestAspect {

    //ProceedingJoinPoint 获取当前请求参数
    @Around("@annotation(com.gtapps.base.utils.AllTest)")
    public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {


        //1.方法执行前的处理，相当于前置通知 因为注解是存在方法上的
        // 获取方法签名
        MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
        // 获取方法
        Method method = methodSignature.getMethod();

        AllTest allTest = method.getAnnotation(AllTest.class);

        //多此一举，肯定会走注解的
//        Annotation[] declaredAnnotations = method.getDeclaredAnnotations();
//        for (Annotation annotation:declaredAnnotations) {
//            if(annotation instanceof AllTest){
//                allTest =   (AllTest)annotation;
//            }
//        }
//
//        if(allTest==null){
//            return null;
//        }

        String operateType = allTest.operateType();
        //执行前置操作
        Object result = null;
        try {
            //让代理方法执行
            result = pjp.proceed();
            // 2.相当于后置通知(方法成功执行之后走这里)
           System.err.println("相当于后置通知(法成功执行之后走这里)");
        } catch (Exception e) {
            // 3.相当于异常通知部分
            System.err.println(e.getMessage());
        } finally {
            // 4.相当于最终通知
            System.err.println("相当于相当于最终通知");
        }
        return result;
    }

       // return null;
    }



